class LoadingUI extends egret.Sprite {
    static singleObj;
    static createObj() {
        if (!LoadingUI.singleObj) {
            LoadingUI.singleObj = new LoadingUI();
        }
        return LoadingUI.singleObj;
    }

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }

    private textField: egret.TextField;
    private progress: egret.DisplayObjectContainer;
    private frontRect: egret.Shape;

    private createView(): void {
        //圆角矩形
        let roundRect = new egret.Shape();
        roundRect.graphics.beginFill(0xffff00);
        roundRect.graphics.drawRoundRect(0, 0, 200, 80, 80, 80);
        roundRect.graphics.endFill();
        this.addChild(roundRect);

        let bg = createRect(this.stage.stageWidth, this.stage.stageHeight, 0x00ffff );
        this.addChild(bg);

        let bgImg = createImg("loading_png");
        this.addChildAt(bgImg,2);

        this.textField = new egret.TextField();
        this.addChild(this.textField);
        this.textField.y = this.stage.stageHeight - this.textField.height - 150;
        this.textField.width = this.stage.stageWidth;
        this.textField.textAlign = "center";
        this.textField.size = 30;

        //实例化进度条容器
        this.progress = new egret.DisplayObjectContainer();
        this.addChild(this.progress);
        this.progress.x = 40;
        this.progress.y = this.stage.stageHeight - 170;

        var bgRect = new egret.Shape();
        bgRect.graphics.beginFill(0xffffff);
        bgRect.graphics.drawRoundRect(0, 0, 560, 5, 5);
        bgRect.graphics.endFill();
        this.progress.addChild(bgRect);
        this.frontRect = createRect(0, 2, 0x00ffff);   //初始化进度条
        this.progress.addChild(this.frontRect);

    }

    public setProgress(current: number, total: number): void {
        var percent = Math.floor((current / total) * 100) + "%";
        // console.log(`${percent}`);
        this.textField.text = `Loading...${percent}`;
        if (this.progress && this.frontRect) {
            this.progress.removeChild(this.frontRect);
            this.frontRect = createRect(current / total * 560, 5, 0x00ff00);
            this.progress.addChild(this.frontRect);
        }
    }

}